package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.ProPurchase;
import com.woniu.entity.PurApprove;
import com.woniu.entity.UserApprove;
import com.woniu.entity.response.ResponseEntity;
import com.woniu.entity.vo.PageProPurchase;
import com.woniu.entity.vo.PagePurApprove;
import com.woniu.entity.vo.ProPurchaseExcel;
import com.woniu.exception.controllerException.ProPurchaseExistException;
import com.woniu.service.IProPurApproveService;
import com.woniu.service.IProPurchaseService;
import com.woniu.util.DateUtils;
import com.woniu.util.ExcelResponseHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *<p>
 *采购审批的前端控制
 *</p>
 * @author ZhouWu
 * @date 2022/9/2 1:10
 */

@RestController
@RequestMapping("/brain/purApprove")
public class PurApproveController {

    @Autowired
    private IProPurApproveService iProPurApproveService;

    //查询全部采购审批
    @RequestMapping("/list")
    public ResponseEntity getAll() {
        List<PurApprove> purApproves = iProPurApproveService.getAll();
        return ResponseEntity.success(purApproves);
    }

    //分页查询采购审批信息
    @RequestMapping("/pageList")
    public ResponseEntity getPurApproveByPage(@RequestBody PagePurApprove pagePurApprove) {
        Page<PurApprove> page =iProPurApproveService.getPurApproveByPage(pagePurApprove.getPageNum(),pagePurApprove.getPageSize(),pagePurApprove.getPurApprove());
        return ResponseEntity.success(page.getRecords(),"分页查询成功");
    }

    //申请审批
    @RequestMapping("/apply")
    public ResponseEntity apply(@RequestBody PurApprove purApprove ) {
        purApprove.setPurApproveStatus("i");
        purApprove.setPurApplyDate(LocalDateTime.now().toString());
        iProPurApproveService.add(purApprove);
        return ResponseEntity.success(null,"添加成功");
    }


    //采购审批通过（这里还需要修改）
    @RequestMapping("/pass")
    public ResponseEntity pass(@RequestBody PurApprove purApprove) {
        int purApproveId = purApprove.getPurApproveId();
        String apdate = DateUtils.getDateTime();
        String apfeedback = purApprove.getPurApproveFeedback();
        iProPurApproveService.pass(purApproveId,apdate,apfeedback);
        return ResponseEntity.success(null,"审批通过");
    }

    //审批未通过（这里还需要修改）
    @RequestMapping("/refuse")
    public ResponseEntity refuse(@RequestBody PurApprove purApprove) {
        int purApproveId = purApprove.getPurApproveId();
        String apdate = DateUtils.getDateTime();
        String apfeedback = purApprove.getPurApproveFeedback();
        iProPurApproveService.refuse(purApproveId,apdate,apfeedback);
        return ResponseEntity.success(null,"审批未通过");
    }

    //根据采购审批id查询审批
    @RequestMapping("/listByPurApprovId/{purApproveId}")
    public ResponseEntity getByPurApprovId(@PathVariable Integer purApproveId ) {
        PurApprove purApprove = iProPurApproveService.getByPurApproveId(purApproveId);
        return  ResponseEntity.success(purApprove,"查询成功");
    }

    //根据用户id查询审批
    @RequestMapping("/listByUserId/{userId}")
    public ResponseEntity getByUserId(@PathVariable Integer userId ) {
        List<PurApprove> purApproves = iProPurApproveService.getByUserId(userId);
        return  ResponseEntity.success(purApproves,"查询成功");
    }

    //根据用户id查询未通过审批
    @RequestMapping("/listRefuseByUserId/{userId}")
    public ResponseEntity getRefuseByUserId(@PathVariable Integer userId ) {
        List<PurApprove> purApproves = iProPurApproveService.getRefuseByPurUserId(userId);
        return  ResponseEntity.success(purApproves,"查询未通过成功");
    }

    //根据部门id查询审批
    @RequestMapping("/listByDeptId/{deptId}")
    public ResponseEntity getByDeptId(@PathVariable Integer deptId ) {
        List<PurApprove> purApproves = iProPurApproveService.getByDeptId(deptId);
        return  ResponseEntity.success(purApproves,"查询成功");
    }

    //删除采购审批
    @RequestMapping("/delete/{purApproveId}")
    public ResponseEntity delete(@PathVariable Integer purApproveId) {
        iProPurApproveService.delete(purApproveId);
        return  ResponseEntity.success(null,"删除成功");
    }

    //根据条件修改采购审批
    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody PurApprove purApprove ) {
        purApprove.setPurApplyDate(LocalDateTime.now().toString());
        purApprove.setPurApproveStatus("i");
        iProPurApproveService.update(purApprove);
        return ResponseEntity.success(null,"修改成功");
    }

    //根据多条件查询审批
    @RequestMapping("/listByCondition")
    public ResponseEntity getByCondition(@RequestBody PurApprove purApprove ) {
        List<PurApprove> purApproves = iProPurApproveService.getPurApproveByCondition(purApprove);
        return ResponseEntity.success(purApproves,"多条件查询成功");
    }

}
